<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="前端,前端基础,服务器配置,Ubuntu,Nginx,LEMP," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="前言拿到一台新装的Linux服务器,该如何配置成一台可以解析动态网页脚本的Web服务器呢? 相信这是很多人都会有的问题。本文将一步一步带你从头开始搭建给予LEMP技术栈的Web服务器,并使其可以正常运行PHP、Nodejs等语言。
所谓的LEMP技术栈,顾名思义其实就是Linux, Nginx, MySQL, PHP的组合。当然,如果喜欢,你也可以不用Nginx,而去选择Apache,这就是所谓的">
<meta property="og:type" content="article">
<meta property="og:title" content="手把手教你从头搭建Web服务器及LEMP环境(使用Ubuntu16.04+Nginx+MySQL+PHP)">
<meta property="og:url" content="http://jiji262.github.io/2016/08/18/ubuntu_1604_web_server_config_LEMP/index.html">
<meta property="og:site_name" content="令狐葱@前端笔记">
<meta property="og:description" content="前言拿到一台新装的Linux服务器,该如何配置成一台可以解析动态网页脚本的Web服务器呢? 相信这是很多人都会有的问题。本文将一步一步带你从头开始搭建给予LEMP技术栈的Web服务器,并使其可以正常运行PHP、Nodejs等语言。
所谓的LEMP技术栈,顾名思义其实就是Linux, Nginx, MySQL, PHP的组合。当然,如果喜欢,你也可以不用Nginx,而去选择Apache,这就是所谓的">
<meta property="og:updated_time" content="2017-01-26T02:06:50.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="手把手教你从头搭建Web服务器及LEMP环境(使用Ubuntu16.04+Nginx+MySQL+PHP)">
<meta name="twitter:description" content="前言拿到一台新装的Linux服务器,该如何配置成一台可以解析动态网页脚本的Web服务器呢? 相信这是很多人都会有的问题。本文将一步一步带你从头开始搭建给予LEMP技术栈的Web服务器,并使其可以正常运行PHP、Nodejs等语言。
所谓的LEMP技术栈,顾名思义其实就是Linux, Nginx, MySQL, PHP的组合。当然,如果喜欢,你也可以不用Nginx,而去选择Apache,这就是所谓的">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"post","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://jiji262.github.io/2016/08/18/ubuntu_1604_web_server_config_LEMP/"/>





  <title> 手把手教你从头搭建Web服务器及LEMP环境(使用Ubuntu16.04+Nginx+MySQL+PHP) | 令狐葱@前端笔记 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?0fd825270d7f51597f963010828c65b8";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-post page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">令狐葱@前端笔记</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://jiji262.github.io/2016/08/18/ubuntu_1604_web_server_config_LEMP/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="令狐葱">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="令狐葱@前端笔记">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="令狐葱@前端笔记" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                手把手教你从头搭建Web服务器及LEMP环境(使用Ubuntu16.04+Nginx+MySQL+PHP)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-08-18T20:10:00+08:00">
                2016-08-18
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/08/18/ubuntu_1604_web_server_config_LEMP/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/08/18/ubuntu_1604_web_server_config_LEMP/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>拿到一台新装的Linux服务器,该如何配置成一台可以解析动态网页脚本的Web服务器呢? 相信这是很多人都会有的问题。本文将一步一步带你从头开始搭建给予LEMP技术栈的Web服务器,并使其可以正常运行PHP、Nodejs等语言。</p>
<p>所谓的LEMP技术栈,顾名思义其实就是Linux, Nginx, MySQL, PHP的组合。当然,如果喜欢,你也可以不用Nginx,而去选择Apache,这就是所谓的LAMP技术栈了。</p>
<p>本文使用Linux版本为Ubuntu 16.04 64位版本。</p>
<p>TL;DR.</p>
<h1 id="Ubuntu服务器基本设置"><a href="#Ubuntu服务器基本设置" class="headerlink" title="Ubuntu服务器基本设置"></a>Ubuntu服务器基本设置</h1><h2 id="使用root登录服务器"><a href="#使用root登录服务器" class="headerlink" title="使用root登录服务器"></a>使用root登录服务器</h2><p>首先我们需要使用root用户登录服务器,当然你可以选择很多工具,诸如SecureCRT或者putty等。这里通过ssh命令行登录:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">local</span>$ ssh root@SERVER_IP_ADDRESS</div></pre></td></tr></table></figure></p>
<p>前面的<code>local$</code>表示是在本地机器操作,在服务器端,该提示符会类似<code>root@my-remote-server:~$</code>这种形式,后边会看到。</p>
<p><em>Tips</em>: 如果使用的是腾讯云，其初始登录默认使用的是用户ubuntu，并不是root。因此，使用<code>ssh ubuntu@your_server_ip</code>登录之后可以修改下root的密码，然后就可以切换到用户root了。</p>
<p>修改密码命令如下：<br><figure class="highlight less"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">ubuntu<span class="variable">@VM-42-158-ubuntu:</span>~$ sudo passwd root</div><div class="line">Enter new UNIX <span class="attribute">password</span>:</div><div class="line">Retype new UNIX <span class="attribute">password</span>:</div><div class="line"><span class="attribute">passwd</span>: password updated successfully</div></pre></td></tr></table></figure></p>
<p>有可能还需要编辑配置文件<code>/etc/ssh/sshd_config</code>中的<code>PermitRootLogin</code>的设置。</p>
<p>好了之后就可以使用<code>su</code>切换到root。<br><figure class="highlight less"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">buntu<span class="variable">@VM-42-158-ubuntu:</span>~$ su</div><div class="line"><span class="attribute">Password</span>:</div><div class="line">root<span class="variable">@VM-42-158-ubuntu</span>:/#</div></pre></td></tr></table></figure></p>
<h2 id="新增超级用户权限用户"><a href="#新增超级用户权限用户" class="headerlink" title="新增超级用户权限用户"></a>新增超级用户权限用户</h2><p>root用户虽然强大,但是正是由于太强大了,所以显得不够安全,因此通常情况下我们需要创建一个新的用户,然后给他设置超级用户(superuser)权限,这样就可以安全的进行各种操作了。</p>
<p>使用root用户登录,做如下操作:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">root@my-remote-server:~$ adduser dennis</div><div class="line">root@my-remote-server:~$ usermod <span class="_">-a</span>G sudo dennis</div></pre></td></tr></table></figure></p>
<p>这里就将新创建的用户加入了sudo用户组,就具有了超级用户的权限。更详细的信息可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file-on-ubuntu-and-centos" target="_blank" rel="external">这个教程</a>。</p>
<p>然后我们就可以以新创建的用户登录了:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">root@my-remote-server:~<span class="comment"># su - dennis</span></div><div class="line">dennis@my-remote-server:~$</div></pre></td></tr></table></figure></p>
<a id="more"></a>
<h2 id="设置SSH-Key认证"><a href="#设置SSH-Key认证" class="headerlink" title="设置SSH Key认证"></a>设置SSH Key认证</h2><p>每次我们通过ssh登录到服务器都需要输入密码,这就是密码认证的方式。为方便起见,我们可以通过ssh key认证来登录。设置之后,我们就不需要在输入密码,就可以直接ssh到服务器。</p>
<p>举例来说,本地机器local需要连接到服务器remote server,我们首先需要在local上生成ssh key:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ssh-keygen</div></pre></td></tr></table></figure></p>
<p>执行之后就会看到,在<code>/Users/localuser/.ssh/</code>目录下会生成两个文件:<code>id_rsa</code>和<code>id_rsa.pub</code>,分别是私钥和公钥。</p>
<p>然后需要将公钥配置到服务器remote server上。一种比较简单的方式是使用ssh-copy-id工具。如果是在Mac上,可以安装<a href="https://github.com/beautifulcode/ssh-copy-id-for-OSX" target="_blank" rel="external">ssh-copy-id-for-OSX</a>. 安装完成后执行<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ssh-copy-id dennis@SERVER_IP_ADDRESS</div></pre></td></tr></table></figure></p>
<p>就配置完成了。另一种方式是手动配置,可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server" target="_blank" rel="external">这里的教程</a>进行操作。文章中还详细介绍了SSH Key的更多内容,可供参考。</p>
<h3 id="禁用密码认证登录方式"><a href="#禁用密码认证登录方式" class="headerlink" title="禁用密码认证登录方式"></a>禁用密码认证登录方式</h3><p>安全起见,我们还可以禁用密码登录的方式。修改配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo vim /etc/ssh/sshd_config</div></pre></td></tr></table></figure></p>
<p>修改如下属性:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">PasswordAuthentication no</div></pre></td></tr></table></figure></p>
<p>然后需要更新下sshd的配置:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo systemctl reload sshd</div></pre></td></tr></table></figure></p>
<p>好了,目前就不能使用密码的方式登录了,只能通过上述ssh key认证的方式登录server了。</p>
<h1 id="LEMP环境搭建及配置"><a href="#LEMP环境搭建及配置" class="headerlink" title="LEMP环境搭建及配置"></a>LEMP环境搭建及配置</h1><h2 id="安装Nginx"><a href="#安装Nginx" class="headerlink" title="安装Nginx"></a>安装Nginx</h2><p>在ubuntu上安装软件很简单,使用<code>apt-get</code>即可。注意由于权限问题,需要在前面加<code>sudo</code>。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo apt-get update</div><div class="line">sudo apt-get install nginx</div></pre></td></tr></table></figure></p>
<h3 id="踩坑提醒"><a href="#踩坑提醒" class="headerlink" title="踩坑提醒"></a>踩坑提醒</h3><h4 id="安装Nginx时出现错误"><a href="#安装Nginx时出现错误" class="headerlink" title="安装Nginx时出现错误"></a>安装Nginx时出现错误</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ sudo apt-get install nginx</div><div class="line">Reading package lists... Done</div><div class="line">Building dependency tree</div><div class="line">Reading state information... Done</div><div class="line">nginx is already the newest version (1.10.0-0ubuntu0.16.04.2).</div><div class="line">0 upgraded, 0 newly installed, 0 to remove and 42 not upgraded.</div><div class="line">2 not fully installed or removed.</div><div class="line">After this operation, 0 B of additional disk space will be used.</div><div class="line">Do you want to <span class="built_in">continue</span>? [Y/n]</div><div class="line">Setting up nginx-core (1.10.0-0ubuntu0.16.04.2) ...</div><div class="line">Job <span class="keyword">for</span> nginx.service failed because the control process exited with error code. See <span class="string">"systemctl status nginx.service"</span> and <span class="string">"journalctl -xe"</span> <span class="keyword">for</span> details.</div><div class="line">invoke-rc.d: initscript nginx, action <span class="string">"start"</span> failed.</div><div class="line">dpkg: error processing package nginx-core (--configure):</div><div class="line"> subprocess installed post-installation script returned error <span class="built_in">exit</span> status 1</div><div class="line">dpkg: dependency problems prevent configuration of nginx:</div><div class="line"> nginx depends on nginx-core (&gt;= 1.10.0-0ubuntu0.16.04.2) | nginx-full (&gt;= 1.10.0-0ubuntu0.16.04.2) | nginx-light (&gt;= 1.10.0-0ubuntu0.16.04.2) | nginx-extras (&gt;= 1.10.0-0ubuntu0.16.04.2); however:</div><div class="line">  Package nginx-core is not configured yet.</div><div class="line">  Package nginx-full is not installed.</div><div class="line">  Package nginx-light is not installed.</div><div class="line">  Package nginx-extras is not installed.</div><div class="line"> nginx depends on nginx-core (&lt;&lt; 1.10.0-0ubuntu0.16.04.2.1~) | nginx-full (&lt;&lt; 1.10.0-0ubuntu0.16.04.2.1~) | nginx-light (&lt;&lt; 1.10.0-0ubuntu0.16.04.2.1~) | nginx-extras (&lt;&lt; 1.10.0-0ubuntu0.16.04.2.1~); however:</div><div class="line">  Package nginx-core is not configured yet.</div><div class="line">  Package nginx-full is not installed.</div><div class="line">  Package nginx-light is not installed.</div><div class="line">  Package nginx-extras is not installed.</div><div class="line"></div><div class="line">dpkg: error processing package nginx (--configure):</div><div class="line"> dependency problems - leaving unconfigured</div><div class="line">Errors were encountered <span class="keyword">while</span> processing:</div><div class="line"> nginx-core</div><div class="line"> nginx</div><div class="line">E: Sub-process /usr/bin/dpkg returned an error code (1)</div></pre></td></tr></table></figure>
<h5 id="原因分析"><a href="#原因分析" class="headerlink" title="原因分析"></a>原因分析</h5><p>Google了一下,发现出现这种错误,一般应该是由于其他应用占用了80端口。可以这样来看下:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">dennis@myserver:~$ sudo netstat -nlp</div><div class="line">sudo: unable to resolve host myserver</div><div class="line">[sudo] password <span class="keyword">for</span> dennis:</div><div class="line">Active Internet connections (only servers)</div><div class="line">Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name</div><div class="line">tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      132/rpcbind</div><div class="line">tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      332/apache2</div><div class="line">tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      269/sshd</div><div class="line">tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      454/master</div><div class="line">tcp6       0      0 :::111                  :::*                    LISTEN      132/rpcbind</div><div class="line">tcp6       0      0 :::22                   :::*                    LISTEN      269/sshd</div></pre></td></tr></table></figure>
<p>果然80端口被默认安装的apache给占掉了,所以需要干掉占用80端口的apache2：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo <span class="built_in">kill</span> -9 332</div></pre></td></tr></table></figure></p>
<h4 id="运行apt-get命令更新或安装软件出现Setting-locale-failed错误"><a href="#运行apt-get命令更新或安装软件出现Setting-locale-failed错误" class="headerlink" title="运行apt-get命令更新或安装软件出现Setting locale failed错误"></a>运行apt-get命令更新或安装软件出现Setting locale failed错误</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">perl: warning: Setting locale failed.</div><div class="line">perl: warning: Please check that your locale settings:</div><div class="line">	LANGUAGE = (<span class="built_in">unset</span>),</div><div class="line">	LC_ALL = (<span class="built_in">unset</span>),</div><div class="line">	LC_CTYPE = <span class="string">"en_US.UTF-8"</span>,</div><div class="line">	LANG = <span class="string">"en_US.UTF-8"</span></div><div class="line">    are supported and installed on your system.</div><div class="line">perl: warning: Falling back to the standard locale (<span class="string">"C"</span>).</div><div class="line">locale: Cannot <span class="built_in">set</span> LC_CTYPE to default locale: No such file or directory</div><div class="line">locale: Cannot <span class="built_in">set</span> LC_MESSAGES to default locale: No such file or directory</div><div class="line">locale: Cannot <span class="built_in">set</span> LC_ALL to default locale: No such file or directory</div></pre></td></tr></table></figure>
<h5 id="解决办法："><a href="#解决办法：" class="headerlink" title="解决办法："></a>解决办法：</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo ap-get update</div><div class="line">apt-get install language-pack-en</div></pre></td></tr></table></figure>
<h3 id="Nginx-常用目录"><a href="#Nginx-常用目录" class="headerlink" title="Nginx 常用目录"></a>Nginx 常用目录</h3><h4 id="内容"><a href="#内容" class="headerlink" title="内容"></a>内容</h4><p><strong>/var/www/html</strong>: 保存web网页内容的默认目录</p>
<h4 id="服务器配置"><a href="#服务器配置" class="headerlink" title="服务器配置"></a>服务器配置</h4><p><strong>/etc/nginx</strong>: nginx 配置目录。所有Nginx相关的配置文件都在这里。<br><strong>/etc/nginx/nginx.conf</strong>: Nginx主配置文件,进行全局默认设置。<br><strong>/etc/nginx/sites-available</strong>: 针对每个”server blocks”独立的配置文件。这里的配置文件并不被直接使用,只有软连接到<code>site-enabled</code>的配置才会真正生效。<br><strong>/etc/nginx/sites-enabled/</strong>: 针对每个”server blocks”独立的配置文件。由<code>sites-available</code>链接过来。<br><strong>/etc/nginx/snippets</strong>: 一些配置脚本片段。</p>
<h4 id="服务器日志"><a href="#服务器日志" class="headerlink" title="服务器日志"></a>服务器日志</h4><p><strong>/var/log/nginx/access.log</strong>: 默认保存所有与web服务交互的请求。<br><strong>/var/log/nginx/error.log</strong>: Nginx的错误日志</p>
<h3 id="Tips"><a href="#Tips" class="headerlink" title="Tips"></a>Tips</h3><h4 id="1-检测Nginx的配置文件是否有问题："><a href="#1-检测Nginx的配置文件是否有问题：" class="headerlink" title="1. 检测Nginx的配置文件是否有问题："></a>1. 检测Nginx的配置文件是否有问题：</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ sudo nginx -t -c /etc/nginx/nginx.conf</div><div class="line">nginx: the configuration file /etc/nginx/nginx.conf syntax is ok</div><div class="line">nginx: configuration file /etc/nginx/nginx.conf <span class="built_in">test</span> is successful</div></pre></td></tr></table></figure>
<h4 id="2-停止apache服务"><a href="#2-停止apache服务" class="headerlink" title="2. 停止apache服务"></a>2. 停止apache服务</h4><p>停掉apache服务的方法有多种，具体参考<a href="http://www.cyberciti.biz/faq/ubuntu-linux-start-restart-stop-apache-web-server/" target="_blank" rel="external">ubuntu-linux-start-restart-stop-apache-web-server</a>. 较为常用的有：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo /etc/init.d/apache2 stop</div></pre></td></tr></table></figure></p>
<p>或者<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ sudo apache2ctl stop</div></pre></td></tr></table></figure></p>
<h4 id="3-取消apache自启动"><a href="#3-取消apache自启动" class="headerlink" title="3. 取消apache自启动"></a>3. 取消apache自启动</h4><p>在ubuntu中apache可能默认会设置为自启动，也就是说kill了之后又会自己起来。可以通过下面的命令取消自启动：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo update-rc.d <span class="_">-f</span> apache2 remove</div></pre></td></tr></table></figure></p>
<p>回复自启动则需要：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo update-rc.d apache2 defaults</div></pre></td></tr></table></figure>
<h4 id="4-Nginx启动与停止"><a href="#4-Nginx启动与停止" class="headerlink" title="4. Nginx启动与停止"></a>4. Nginx启动与停止</h4><p>和apache的启动和停止一样，Nginx的启动和停止服务<a href="http://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/" target="_blank" rel="external">也有多种方式</a>，常用的有：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">//To stop your web server, you can <span class="built_in">type</span>:</div><div class="line">sudo systemctl stop nginx</div><div class="line"></div><div class="line">//To start the web server when it is stopped, <span class="built_in">type</span>:</div><div class="line">sudo systemctl start nginx</div><div class="line"></div><div class="line">//To stop and <span class="keyword">then</span> start the service again, <span class="built_in">type</span>:</div><div class="line">sudo systemctl restart nginx</div><div class="line"></div><div class="line">//If you are simply making configuration changes, Nginx can often reload without dropping connections. To <span class="keyword">do</span> this, this <span class="built_in">command</span> can be used:</div><div class="line">sudo systemctl reload nginx</div><div class="line"></div><div class="line">//By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can <span class="built_in">disable</span> this behavior by typing:</div><div class="line">sudo systemctl <span class="built_in">disable</span> nginx</div><div class="line"></div><div class="line">//To re-enable the service to start up at boot, you can <span class="built_in">type</span>:</div><div class="line">sudo systemctl <span class="built_in">enable</span> nginx</div></pre></td></tr></table></figure></p>
<p>或者<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo /etc/init.d/nginx restart</div><div class="line">sudo service nginx restart</div></pre></td></tr></table></figure></p>
<h4 id="5-查看本机ip地址"><a href="#5-查看本机ip地址" class="headerlink" title="5. 查看本机ip地址"></a>5. 查看本机ip地址</h4><figure class="highlight nginx"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="attribute">ip</span> addr show venet0 | grep inet | awk <span class="string">'&#123; print <span class="variable">$2</span>; &#125;'</span> | sed <span class="string">'s/\/.*$//'</span></div></pre></td></tr></table></figure>
<p>其中venet0可能为eth0，可以通过<code>ip addr</code> 或者<code>ifconfig</code>看下。</p>
<p>或者可以这样：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:/$ curl http://icanhazip.com</div><div class="line">xxx.xxx.xxx.xxx</div></pre></td></tr></table></figure></p>
<h4 id="6-检查Nginx的状态"><a href="#6-检查Nginx的状态" class="headerlink" title="6. 检查Nginx的状态:"></a>6. 检查Nginx的状态:</h4><pre><code><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:/var/www$ sudo systemctl status nginx</div><div class="line">● nginx.service - A high performance web server and a reverse proxy server</div><div class="line">   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)</div><div class="line">   Active: active (running) since Wed 2016-08-17 14:39:33 CST; 2h 43min ago</div><div class="line"> Main PID: 5221 (nginx)</div><div class="line">   CGroup: /system.slice/nginx.service</div><div class="line">           ├─ 5221 nginx: master process /usr/sbin/nginx -g daemon on; master_process on</div><div class="line">           ├─15622 nginx: worker process</div><div class="line">           └─15623 nginx: worker process</div><div class="line"></div><div class="line">Aug 17 14:39:33 my-remote-server systemd[1]: Stopped A high performance web server and a reverse proxy server.</div><div class="line">Aug 17 14:39:33 my-remote-server systemd[1]: Starting A high performance web server and a reverse proxy server...</div><div class="line">Aug 17 14:39:33 my-remote-server systemd[1]: nginx.service: Failed to <span class="built_in">read</span> PID from file /run/nginx.pid: Invalid argument</div><div class="line">Aug 17 14:39:33 my-remote-server systemd[1]: Started A high performance web server and a reverse proxy server.</div><div class="line">Aug 17 15:52:19 my-remote-server systemd[1]: Reloading A high performance web server and a reverse proxy server.</div><div class="line">Aug 17 15:52:19 my-remote-server systemd[1]: Reloaded A high performance web server and a reverse proxy server.</div><div class="line">Aug 17 16:34:05 my-remote-server systemd[1]: Reloading A high performance web server and a reverse proxy server.</div><div class="line">Aug 17 16:34:05 my-remote-server systemd[1]: Reloaded A high performance web server and a reverse proxy server.</div></pre></td></tr></table></figure>
</code></pre><h2 id="安装MySQL"><a href="#安装MySQL" class="headerlink" title="安装MySQL"></a>安装MySQL</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo apt-get update</div><div class="line">sudo apt-get install mysql-server</div></pre></td></tr></table></figure>
<h3 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo mysql_secure_installation</div></pre></td></tr></table></figure>
<p>查看MySQL的版本：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql --version</div></pre></td></tr></table></figure></p>
<h3 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h3><p>在5.7.6以前的版本：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo mysql_install_db</div></pre></td></tr></table></figure></p>
<p>5.7.6及之后的版本：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo mysqld --initialize</div></pre></td></tr></table></figure></p>
<p>如果是如上面那样通过apt-get安装的，这一步通常已经被做了。因此会有如下错误提示：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[ERROR] --initialize specified but the data directory has files <span class="keyword">in</span> it. Aborting.</div></pre></td></tr></table></figure></p>
<h3 id="MySQL的启动和停止"><a href="#MySQL的启动和停止" class="headerlink" title="MySQL的启动和停止"></a>MySQL的启动和停止</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo /etc/init.d/mysql start</div><div class="line">sudo /etc/init.d/mysql stop</div><div class="line">sudo /etc/init.d/mysql restart</div></pre></td></tr></table></figure>
<p>或者：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo start mysql</div><div class="line">sudo stop mysql</div><div class="line">sudo restart mysql</div></pre></td></tr></table></figure></p>
<p>这种方式使用了“upstart” (其实就是/etc/init.d/mysql的软连接)。</p>
<h3 id="测试连接"><a href="#测试连接" class="headerlink" title="测试连接"></a>测试连接</h3><p>可以通过<code>mysqladmin -p -u root version</code>命令来测试一下mysql连接是否正常。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:/var/www/html$ mysqladmin -p -u root version</div><div class="line">Enter password:</div><div class="line">mysqladmin  Ver 8.42 Distrib 5.7.13, <span class="keyword">for</span> Linux on x86_64</div><div class="line">Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.</div><div class="line"></div><div class="line">Oracle is a registered trademark of Oracle Corporation and/or its</div><div class="line">affiliates. Other names may be trademarks of their respective</div><div class="line">owners.</div><div class="line"></div><div class="line">Server version		5.7.13-0ubuntu0.16.04.2</div><div class="line">Protocol version	10</div><div class="line">Connection		Localhost via UNIX socket</div><div class="line">UNIX socket		/var/run/mysqld/mysqld.sock</div><div class="line">Uptime:			19 min 14 sec</div><div class="line"></div><div class="line">Threads: 1  Questions: 8  Slow queries: 0  Opens: 115  Flush tables: 1  Open tables: 34  Queries per second avg: 0.006</div></pre></td></tr></table></figure>
<h2 id="安装PHP"><a href="#安装PHP" class="headerlink" title="安装PHP"></a>安装PHP</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo apt-get -y install php7.0-fpm php7.0-mysql</div></pre></td></tr></table></figure>
<h3 id="配置PHP设置"><a href="#配置PHP设置" class="headerlink" title="配置PHP设置"></a>配置PHP设置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo vim /etc/php/7.0/fpm/php.ini</div></pre></td></tr></table></figure>
<p>打开配置文件，找到<code>cgi.fix_pathinfo</code>，修改为：<br><figure class="highlight fix"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="attr">cgi.fix_pathinfo</span>=<span class="string">0</span></div></pre></td></tr></table></figure></p>
<p>然后重启php环境：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo systemctl restart php7.0-fpm</div></pre></td></tr></table></figure></p>
<h3 id="Nginx中配置支持PHP"><a href="#Nginx中配置支持PHP" class="headerlink" title="Nginx中配置支持PHP"></a>Nginx中配置支持PHP</h3><p>打开Nginx服务块配置文件：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo vim /etc/nginx/sites-available/default</div></pre></td></tr></table></figure></p>
<p>修改文件为(具体修改可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04" target="_blank" rel="external">这里的说明</a>)：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    listen 80 default_server;</div><div class="line">    listen [::]:80 default_server;</div><div class="line"></div><div class="line">    root /var/www/html;</div><div class="line">    index index.php index.html index.htm index.nginx-debian.html;</div><div class="line"></div><div class="line">    server_name server_domain_or_IP;</div><div class="line"></div><div class="line">    location / &#123;</div><div class="line">        try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ =404;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    location ~ \.php$ &#123;</div><div class="line">        include snippets/fastcgi-php.conf;</div><div class="line">        fastcgi_pass unix:/run/php/php7.0-fpm.sock;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    location ~ /\.ht &#123;</div><div class="line">        deny all;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>修改完保存之后可以使用<code>sudo nginx -t</code>命令确认一下配置文件的正确性。<br>然后使用命令<code>sudo systemctl reload nginx</code>重启Nginx。</p>
<h3 id="测试一下"><a href="#测试一下" class="headerlink" title="测试一下"></a>测试一下</h3><p>新建文件<code>/var/www/html/info.php</code>，内容如下：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">phpinfo();</div></pre></td></tr></table></figure></p>
<p>然后访问 <code>http://server_domain_or_IP/info.php</code>查看结果。</p>
<h1 id="安装Wordpress应用程序-可选"><a href="#安装Wordpress应用程序-可选" class="headerlink" title="安装Wordpress应用程序 (可选)"></a>安装Wordpress应用程序 (可选)</h1><p>至此LEMP环境已经搭建完毕,下面我们简单介绍下在该环境上安装最为流行的博客程序wordpress的过程。本节仅供参考,如果不需要,可以不安装。</p>
<h2 id="数据库及环境配置"><a href="#数据库及环境配置" class="headerlink" title="数据库及环境配置"></a>数据库及环境配置</h2><h3 id="创建数据库及新的数据库用户"><a href="#创建数据库及新的数据库用户" class="headerlink" title="创建数据库及新的数据库用户"></a>创建数据库及新的数据库用户</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql -u root -p</div></pre></td></tr></table></figure>
<p>用密码登录成功之后执行下面的sql语句创建一个数据库，名叫wordpress<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">CREATE</span> <span class="keyword">DATABASE</span> wordpress <span class="keyword">DEFAULT</span> <span class="built_in">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_unicode_ci;</div></pre></td></tr></table></figure></p>
<p>安全起见，我们为这个数据库创建一个单独的用户：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">GRANT</span> ALL <span class="keyword">ON</span> wordpress.* <span class="keyword">TO</span> <span class="string">'wordpressuser'</span>@<span class="string">'localhost'</span> <span class="keyword">IDENTIFIED</span> <span class="keyword">BY</span> <span class="string">'password'</span>;</div><div class="line"><span class="keyword">FLUSH</span> <span class="keyword">PRIVILEGES</span>;</div><div class="line">EXIT;</div></pre></td></tr></table></figure></p>
<h4 id="在外网通过IP直接访问操作数据库"><a href="#在外网通过IP直接访问操作数据库" class="headerlink" title="在外网通过IP直接访问操作数据库"></a>在外网通过IP直接访问操作数据库</h4><p>默认情况下,我们只能在服务器上通过<code>localhost</code>访问MySQL,如果需要通过IP从外网访问,需要进行相应的设置。</p>
<p>1) 修改授权</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">GRANT</span> ALL <span class="keyword">ON</span> wordpress.* <span class="keyword">TO</span> <span class="string">'wordpressuser'</span>@<span class="string">'%'</span> <span class="keyword">IDENTIFIED</span> <span class="keyword">BY</span> <span class="string">'password'</span>;</div><div class="line"><span class="keyword">FLUSH</span> <span class="keyword">PRIVILEGES</span>;</div><div class="line">EXIT;</div></pre></td></tr></table></figure>
<p>如果需要,还可以指定特定的IP地址(替换上面的%即可)。</p>
<p>或者可以直接修改表:<br><figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">mysql -u root -p</div><div class="line"></div><div class="line">mysql&gt; use mysql;</div><div class="line"></div><div class="line">mysql&gt; update user set host = <span class="emphasis">'%'</span> where user = <span class="emphasis">'root'</span>;</div><div class="line"></div><div class="line"><span class="section">mysql&gt; select user, host from user;</span></div><div class="line">+------------------+-----------+</div><div class="line"><span class="section">| user             | host      |</span></div><div class="line">+------------------+-----------+</div><div class="line">| mysql.sys        | localhost |</div><div class="line">| root             | localhost |</div><div class="line"><span class="section">| wordpressuser    | %         |</span></div><div class="line">+------------------+-----------+</div><div class="line">3 rows in set (0.00 sec)</div></pre></td></tr></table></figure></p>
<p>2) 修改配置文件</p>
<p>配置文件位置为<code>/etc/mysql/mysql.conf.d/mysqld.cnf</code>,注释掉其中一行:<br><figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"># bind-address  =<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></div></pre></td></tr></table></figure></p>
<p>重启MySQL即可:<br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo service mysql <span class="built_in">restart</span></div></pre></td></tr></table></figure></p>
<h3 id="配置Nginx"><a href="#配置Nginx" class="headerlink" title="配置Nginx"></a>配置Nginx</h3><p>打开配置文件：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo vim /etc/nginx/sites-available/default</div></pre></td></tr></table></figure></p>
<p>作如下修改（具体可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04" target="_blank" rel="external">这里的说明</a>）<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    . . .</div><div class="line"></div><div class="line">    location = /favicon.ico &#123; <span class="built_in">log</span>_not_found off; access_<span class="built_in">log</span> off; &#125;</div><div class="line">    location = /robots.txt &#123; <span class="built_in">log</span>_not_found off; access_<span class="built_in">log</span> off; allow all; &#125;</div><div class="line">    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ &#123;</div><div class="line">        expires max;</div><div class="line">        <span class="built_in">log</span>_not_found off;</div><div class="line">    &#125;</div><div class="line">    . . .</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>对try_files做如下修改：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    . . .</div><div class="line">    location / &#123;</div><div class="line">        <span class="comment">#try_files $uri $uri/ =404;</span></div><div class="line">        try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ /index.php<span class="variable">$is_args</span><span class="variable">$args</span>;</div><div class="line">    &#125;</div><div class="line">    . . .</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>然后检查配置文件的正确性，并重启Nginx：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo nginx -t</div><div class="line">//If no errors were reported, reload Nginx by typing:</div><div class="line">sudo systemctl reload nginx</div></pre></td></tr></table></figure></p>
<h3 id="安装额外的PHP扩展"><a href="#安装额外的PHP扩展" class="headerlink" title="安装额外的PHP扩展"></a>安装额外的PHP扩展</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo apt-get update</div><div class="line">sudo apt-get install php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc</div></pre></td></tr></table></figure>
<p>安装完之后需要重启PHP-FPM,来使新的设置生效。<br><figure class="highlight smali"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo<span class="keyword"> system</span>ctl restart php7.0-fpm</div></pre></td></tr></table></figure></p>
<h2 id="安装Wordpress"><a href="#安装Wordpress" class="headerlink" title="安装Wordpress"></a>安装Wordpress</h2><h3 id="获取并安装wordpress源码"><a href="#获取并安装wordpress源码" class="headerlink" title="获取并安装wordpress源码"></a>获取并安装wordpress源码</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /tmp</div><div class="line">curl -O https://wordpress.org/latest.tar.gz</div><div class="line">tar xzvf latest.tar.gz</div><div class="line">cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php</div><div class="line">mkdir /tmp/wordpress/wp-content/upgrade</div><div class="line">sudo cp <span class="_">-a</span> /tmp/wordpress/. /var/www/html</div></pre></td></tr></table></figure>
<h3 id="调整文件权限和所有权"><a href="#调整文件权限和所有权" class="headerlink" title="调整文件权限和所有权"></a>调整文件权限和所有权</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo chown -R dennis:www-data /var/www/html</div></pre></td></tr></table></figure>
<p>我们还需要设置setgid来使目录下新创建的文件,和父级目录具有相同的权限:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo find /var/www/html -type d -exec chmod g+s &#123;&#125; \;</div></pre></td></tr></table></figure></p>
<p>另外还需要对一些特殊的目录做处理:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /var/www/html</div><div class="line">sudo chmod g+w ./wp-content</div><div class="line">sudo chmod -R g+w ./wp-content/themes</div><div class="line">sudo chmod -R g+w ./wp-content/plugins</div></pre></td></tr></table></figure></p>
<h3 id="设置secure-key"><a href="#设置secure-key" class="headerlink" title="设置secure key"></a>设置secure key</h3><p>我们可以用wordpress官方提供的工具生成secure key:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">curl <span class="_">-s</span> https://api.wordpress.org/secret-key/1.1/salt/</div></pre></td></tr></table></figure></p>
<p>运行后得到如下结果:<br><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">define(<span class="string">'AUTH_KEY'</span>,         <span class="string">'b?=x1eCLLa9c6f]%=8A$D^P=,y$+#)|XV2ffFo-sq8xY8M-a|6IE0_T-|!O.*Esa'</span>);</div><div class="line">define(<span class="string">'SECURE_AUTH_KEY'</span>,  <span class="string">'mF5CQ|m&#123;(tWQQhK+_&gt;d4UbJ5VU|], c)5^!wYbQ1WU+tBk8tFh]_&lt;p#yZ|x;T&#123;L%'</span>);</div><div class="line">define(<span class="string">'LOGGED_IN_KEY'</span>,    <span class="string">'(J&lt;=P5mY3?&gt;bqMqwk!]O=R+|]=8q^Hj/_+Dro=`-8XA[lBUQnt+Wk2MJnlC?$k&amp;L'</span>);</div><div class="line">define(<span class="string">'NONCE_KEY'</span>,        <span class="string">'y,D2p24;-_g7-(Tu&lt;X0HEPU:_(&#123;?JA4giAH@#&lt;WPiVc=P%XwzB1.e|x#,l]1n2CO'</span>);</div><div class="line">define(<span class="string">'AUTH_SALT'</span>,        <span class="string">')+fC8PF&amp;FwD[*ux[ |YXxF-*!ds$uuy3TCzp|+-v_vt*-ox-6|A+A!]A*tJo^De='</span>);</div><div class="line">define(<span class="string">'SECURE_AUTH_SALT'</span>, <span class="string">'4Ms!kC&gt;.Y2*6fE+?;fE=&gt;0BR~cB5J;C/6Sn177c(p%Q(Q6a-&#123;I&amp;[N,2Tn!ly.GgW'</span>);</div><div class="line">define(<span class="string">'LOGGED_IN_SALT'</span>,   <span class="string">'0nu||~mIX-++A;uS3bWde2-=A2+8=`c_(6JD_hJPf@9DiTiAu--W[wFb&#125;+:P|[[+'</span>);</div><div class="line">define(<span class="string">'NONCE_SALT'</span>,       <span class="string">' F=UmQao!jv(|#Di=A$Z6(l^_|z=wTnI2/P8&lt;l7BO/IfiqX03!+hMqDa*6|hxog3'</span>);</div></pre></td></tr></table></figure></p>
<p>拷贝到<code>wp-config.php</code>文件中替换相应的内容。</p>
<h3 id="设置数据库信息及文件操作权限"><a href="#设置数据库信息及文件操作权限" class="headerlink" title="设置数据库信息及文件操作权限"></a>设置数据库信息及文件操作权限</h3><p><code>wp-config.php</code>文件中修改数据库信息,并添加<code>FS_METHOD</code>以便于Wordpress可以访问文件系统进行文件的读写操作,这在安装插件的时候会很有用。<br><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">define(<span class="string">'DB_NAME'</span>, <span class="string">'wordpress'</span>);</div><div class="line"></div><div class="line"><span class="comment">/** MySQL database username */</span></div><div class="line">define(<span class="string">'DB_USER'</span>, <span class="string">'wordpressuser'</span>);</div><div class="line"></div><div class="line"><span class="comment">/** MySQL database password */</span></div><div class="line">define(<span class="string">'DB_PASSWORD'</span>, <span class="string">'password'</span>);</div><div class="line"></div><div class="line">define(<span class="string">'FS_METHOD'</span>, <span class="string">'direct'</span>);</div></pre></td></tr></table></figure></p>
<p>然后我们就可以访问<a href="http://server_domain_or_IP进行wordpress的安装了。" target="_blank" rel="external">http://server_domain_or_IP进行wordpress的安装了。</a></p>
<h1 id="LEMP环境上配置多个站点"><a href="#LEMP环境上配置多个站点" class="headerlink" title="LEMP环境上配置多个站点"></a>LEMP环境上配置多个站点</h1><p>通常情况下我们需要在Web服务器上部署多个站点,使用多个不同的域名。下面将详细介绍在我们的LEMP环境上如何配置。</p>
<h2 id="设置新的文档目录"><a href="#设置新的文档目录" class="headerlink" title="设置新的文档目录"></a>设置新的文档目录</h2><p>默认情况下,在Ubuntu上的Nginx已经默认创建了一个server block,其文档目录为/var/www/html(我们在上面安装wordpress的时候使用的就是这个默认的server block)。</p>
<p>如果我们需要部署多个站点,那么就需要创建多个不同的server block。假如我们需要部署两个网站:<br><figure class="highlight css"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="selector-tag">example</span><span class="selector-class">.com</span></div><div class="line"><span class="selector-tag">test</span><span class="selector-class">.com</span></div></pre></td></tr></table></figure></p>
<p>这样我们就需要设置两个新的文档目录。为统一起见,我们使用<code>xxx.com/html</code>这种目录结构形式:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo mkdir -p /var/www/example.com/html</div><div class="line">sudo mkdir -p /var/www/test.com/html</div></pre></td></tr></table></figure></p>
<p>修改一下这两个文档目录的权限:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo chown -R <span class="variable">$USER</span>:<span class="variable">$USER</span> /var/www/example.com/html</div><div class="line">sudo chown -R <span class="variable">$USER</span>:<span class="variable">$USER</span> /var/www/test.com/html</div></pre></td></tr></table></figure></p>
<p>这里用到了环境变量<code>$USER</code>,请确保没有使用root账号进行操作。<br><figure class="highlight less"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dennis<span class="variable">@my-remote-server:</span>~$ echo $USER</div><div class="line">dennis</div></pre></td></tr></table></figure></p>
<p>至此文档目录应该配置好了。如果需要,我们可以通过下面的命令设置一下上层目录的权限:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo chmod -R 755 /var/www</div></pre></td></tr></table></figure></p>
<p>需要提醒的是,暂时不需要担心这两个测试域名是否可以访问的问题,后边我们会介绍在本地浏览器如何访问这两个测试域名。</p>
<h2 id="为每个站点创建测试文件"><a href="#为每个站点创建测试文件" class="headerlink" title="为每个站点创建测试文件"></a>为每个站点创建测试文件</h2><p>创建文件<code>/var/www/example.com/html/index.html</code>,内容为:<br><figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Welcome to Example.com!<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Success!  The example.com server block is working!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>同样,创建文件<code>/var/www/test.com/html/index.html</code>,内容为:<br>   <figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Welcome to Test.com!<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Success!  The test.com server block is working!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h2 id="为每个站点创建server-block文件"><a href="#为每个站点创建server-block文件" class="headerlink" title="为每个站点创建server block文件"></a>为每个站点创建server block文件</h2><p>如前所述,默认情况下Nginx已经配置了一个默认的server block,因此我们可以将默认的server block配置文件拷贝过来稍作修改:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com</div></pre></td></tr></table></figure></p>
<p>打开文件:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo vim /etc/nginx/sites-available/example.com</div></pre></td></tr></table></figure></p>
<p>其内容如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">        listen 80 default_server;</div><div class="line">        listen [::]:80 default_server;</div><div class="line"></div><div class="line">        root /var/www/html;</div><div class="line">        index index.html index.htm index.nginx-debian.html;</div><div class="line"></div><div class="line">        server_name _;</div><div class="line"></div><div class="line">        location / &#123;</div><div class="line">                try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ =404;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>对其内容稍作修改,修改之后内容如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">        listen 80;</div><div class="line">        listen [::]:80;</div><div class="line"></div><div class="line">        root /var/www/example.com/html;</div><div class="line">        index index.html index.htm index.nginx-debian.html;</div><div class="line"></div><div class="line">        server_name example.com www.example.com;</div><div class="line"></div><div class="line">        location / &#123;</div><div class="line">                try_files <span class="variable">$uri</span> <span class="variable">$uri</span>/ =404;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>主要修改了几个地方:</p>
<ul>
<li>去掉了default_server字眼。一台服务器上只能有一个default_server的配置,因此我们保留系统最初的设置为默认设置。</li>
<li>修改root目录.</li>
<li>修改server_name.</li>
</ul>
<p>针对第二个站点<code>test.com</code>也做类似修改。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com</div><div class="line">sudo vim /etc/nginx/sites-available/test.com</div></pre></td></tr></table></figure></p>
<h2 id="激活两个站点的server-block"><a href="#激活两个站点的server-block" class="headerlink" title="激活两个站点的server block"></a>激活两个站点的server block</h2><p>使用如下命令:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo ln <span class="_">-s</span> /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/</div><div class="line">sudo ln <span class="_">-s</span> /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/</div></pre></td></tr></table></figure></p>
<p>这样这些文件(链接)就位于激活的目录内了。到目前为止我们有3个激活了的server block了。服务器根据listen指令和server_name来确定该访问那个目录。</p>
<ul>
<li>example.com: 响应来自<code>example.com</code>以及<code>www.example.com</code>的请求</li>
<li>test.com: 响应来自<code>test.com</code>以及<code>www.test.com</code>的请求</li>
<li>default: 响应没有匹配到上面两个规则的80端口的请求。</li>
</ul>
<p>另外,还需要在nginx配置文件<code>/etc/nginx/nginx.conf</code>中设置下<code>server_names_hash_bucket_size</code>:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">http &#123;</div><div class="line">    . . .</div><div class="line"></div><div class="line">    server_names_<span class="built_in">hash</span>_bucket_size 64;</div><div class="line"></div><div class="line">    . . .</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>然后检查下nginx配置文件的正确性:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo nginx -t</div></pre></td></tr></table></figure></p>
<p>重启一下nginx使修改生效:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo systemctl restart nginx</div></pre></td></tr></table></figure></p>
<p>关于Nginx更多的指令介绍可以<a href="https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms" target="_blank" rel="external">参考这个教程</a>。</p>
<h2 id="本地测试"><a href="#本地测试" class="headerlink" title="本地测试"></a>本地测试</h2><p>由于<code>example.com</code>和<code>test.com</code>这两个域名并非我们真实拥有的域名,因此需要在本地机器修改下hosts来测试访问。<br>以Mac为例,修改<code>/etc/hosts</code>文件:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">127.0.0.1   localhost</div><div class="line">. . .</div><div class="line"></div><div class="line">XXX.XXX.XXX.XXX example.com www.example.com</div><div class="line">XXX.XXX.XXX.XXX test.com www.test.com</div></pre></td></tr></table></figure></p>
<p>前面的<code>XXX.XXX.XXX.XXX</code>即为服务器的外网IP。<br>现在我们就可以直接访问<code>example.com</code>和<code>test.com</code>来查看这两个站点了。</p>
<p>Tips: 关于真实域名相关的设置,有需要可以<a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean" target="_blank" rel="external">参考这篇文章</a>.</p>
<h2 id="配置二级子域名及代理访问"><a href="#配置二级子域名及代理访问" class="headerlink" title="配置二级子域名及代理访问"></a>配置二级子域名及代理访问</h2><p>举例来说，比如我们在我的服务器上有两个web程序，一个是之前的 <code>example.com</code>，另一个程序是nodejs应用，使用端口5555，需要映射到二级域名 <code>demo.example.com</code>。我们应该如何设置呢？</p>
<h3 id="1）添加域名解析"><a href="#1）添加域名解析" class="headerlink" title="1）添加域名解析"></a>1）添加域名解析</h3><p>这个步骤不多说了，在域名服务提供商网站添加一条A记录，设置域名demo.example.com。</p>
<h3 id="2）修改Nginx配置文件"><a href="#2）修改Nginx配置文件" class="headerlink" title="2）修改Nginx配置文件"></a>2）修改Nginx配置文件</h3><p>就是我们上面提到的文件<code>/etc/nginx/sites-available/example.com</code>,添加如下内容：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="section">server</span> &#123;  </div><div class="line">    <span class="attribute">listen</span> <span class="number">80</span>;</div><div class="line">    <span class="attribute">server_name</span> demo.example.com;</div><div class="line"></div><div class="line">    <span class="attribute">location</span> / &#123;</div><div class="line">        <span class="attribute">proxy_set_header</span>   X-Real-IP <span class="variable">$remote_addr</span>;</div><div class="line">        <span class="attribute">proxy_set_header</span>   Host      <span class="variable">$http_host</span>;</div><div class="line">        <span class="attribute">proxy_pass</span>         http://127.0.0.1:5555;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>然后重启Nginx即可。</p>
<h1 id="Ubuntu上安装和部署Node-js应用"><a href="#Ubuntu上安装和部署Node-js应用" class="headerlink" title="Ubuntu上安装和部署Node.js应用"></a>Ubuntu上安装和部署Node.js应用</h1><h2 id="安装Node-js"><a href="#安装Node-js" class="headerlink" title="安装Node.js"></a>安装Node.js</h2><h3 id="通过apt-get的方式安装"><a href="#通过apt-get的方式安装" class="headerlink" title="通过apt-get的方式安装"></a>通过apt-get的方式安装</h3><p>在ubuntu上可以使用apt-get安装Node.js:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo apt-get update</div><div class="line">sudo apt-get install nodejs</div><div class="line">sudo apt-get install npm</div></pre></td></tr></table></figure></p>
<p>不过通过这种方式安装的Node.js会存在两个问题:</p>
<ul>
<li>nodejs的版本比较老,目前我看到的是v4.2.6。</li>
<li>在ubuntu上由于node这个名字被其他程序占用,因此要使用Node.js需要使用nodejs。<br>如:<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ nodejs --version</div><div class="line">v4.2.6</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="通过NVM安装"><a href="#通过NVM安装" class="headerlink" title="通过NVM安装"></a>通过NVM安装</h3><p>NVM顾名思义就是Node.js版本管理器(“Node.js version manager”),它可以让我们安装多个Node.js版本并且可以方便的随意切换。</p>
<p>安装NVM,首先需要安装一些依赖包:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo apt-get update</div><div class="line">sudo apt-get install build-essential libssl-dev</div></pre></td></tr></table></figure></p>
<p>然后从<a href="https://github.com/creationix/nvm" target="_blank" rel="external">NVM的Github repo</a>里拿到安装脚本。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">curl <span class="_">-s</span>L https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh -o install_nvm.sh</div></pre></td></tr></table></figure></p>
<p>目前我看到的最新版本是0.31.4. 你可以修改为当前的最新版本。</p>
<p>然后执行脚本:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bash install_nvm.sh</div></pre></td></tr></table></figure></p>
<p>该脚本会将NVM安装在<code>~/.nvm</code>目录,并且会对<code>~/.profile</code>文件做一些必要的修改,我们需要使其修改立即生效:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span> ~/.profile</div></pre></td></tr></table></figure></p>
<p>然后可以通过下面的命令查看当前可用的Node.js版本:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ nvm ls-remote</div><div class="line">        v5.10.0</div><div class="line">        v5.10.1</div><div class="line">        v5.11.0</div><div class="line">        v5.11.1</div><div class="line">        v5.12.0</div><div class="line">         v6.0.0</div><div class="line">         v6.1.0</div><div class="line">         v6.2.0</div><div class="line">         v6.2.1</div><div class="line">         v6.2.2</div><div class="line">         v6.3.0</div><div class="line">         v6.3.1</div><div class="line">         v6.4.0</div></pre></td></tr></table></figure></p>
<p>最新版本为v6.4.0.<br>然后通过下面的命令安装最新版本:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nvm install 6.4.0</div></pre></td></tr></table></figure></p>
<p>通常情况下,NVM会选择最近安装的版本来使用。当然我们也可以使用<code>nvm use 6.4.0</code>命令来切换版本。<br>可以使用命令<code>nvm ls</code>来查看当前环境已经安装的版本:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ nvm ls</div><div class="line">-&gt;       v6.4.0</div><div class="line">default -&gt; 6.4.0 (-&gt; v6.4.0)</div><div class="line">node -&gt; stable (-&gt; v6.4.0) (default)</div><div class="line">stable -&gt; 6.4 (-&gt; v6.4.0) (default)</div><div class="line">iojs -&gt; N/A (default)</div></pre></td></tr></table></figure></p>
<h3 id="通过添加PPA的方式安装"><a href="#通过添加PPA的方式安装" class="headerlink" title="通过添加PPA的方式安装"></a>通过添加PPA的方式安装</h3><p>这种方式暂时不做介绍,有兴趣可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04" target="_blank" rel="external">这里的教程</a>。</p>
<h2 id="创建一个简单的Node-js应用"><a href="#创建一个简单的Node-js应用" class="headerlink" title="创建一个简单的Node.js应用"></a>创建一个简单的Node.js应用</h2><p>我们创建一个简单的Node.js应用用于测试。新建一个文件<code>hello.js</code>:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/usr/bin/env nodejs</span></div><div class="line"><span class="keyword">var</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</div><div class="line">http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>) </span>&#123;</div><div class="line">  res.writeHead(<span class="number">200</span>, &#123;<span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span>&#125;);</div><div class="line">  res.end(<span class="string">'Hello World\n'</span>);</div><div class="line">&#125;).listen(<span class="number">8080</span>, <span class="string">'localhost'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'Server running at http://localhost:8080/'</span>);</div></pre></td></tr></table></figure></p>
<p>修改其可执行权限,并执行:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chmod +x ./hello.js</div><div class="line">./hello.js</div></pre></td></tr></table></figure></p>
<p>可以看到下面的提示:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Server running at http://localhost:8080/</div></pre></td></tr></table></figure></p>
<p>说明服务启动正常,这时候可以通过<a href="http://:8080/IP_address:8080" target="_blank" rel="external">http://:8080/IP_address:8080</a> 来访问该应用。</p>
<h2 id="使用PM2管理Node-js应用"><a href="#使用PM2管理Node-js应用" class="headerlink" title="使用PM2管理Node.js应用"></a>使用PM2管理Node.js应用</h2><p>PM2是优秀的Node.js应用管理工具,使用它可以轻松的管理服务器上的Node.js应用,并使其保持后台运行状态。</p>
<h3 id="安装PM2"><a href="#安装PM2" class="headerlink" title="安装PM2"></a>安装PM2</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo npm install -g pm2</div></pre></td></tr></table></figure>
<h3 id="使用PM2"><a href="#使用PM2" class="headerlink" title="使用PM2"></a>使用PM2</h3><h4 id="启动Node-js应用"><a href="#启动Node-js应用" class="headerlink" title="启动Node.js应用"></a>启动Node.js应用</h4><p>可以使用<code>pm2 start</code>命令启动我们刚才创建的示例应用。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ pm2 start hello.js</div><div class="line">[PM2] Spawning PM2 daemon</div><div class="line">[PM2] PM2 Successfully daemonized</div><div class="line">[PM2] Starting hello.js <span class="keyword">in</span> fork_mode (1 instance)</div><div class="line">[PM2] Done.</div><div class="line">┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐</div><div class="line">│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │</div><div class="line">├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤</div><div class="line">│ hello    │ 0  │ fork │ 8689 │ online │ 0       │ 0s     │ 14.566 MB   │ disabled │</div><div class="line">└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘</div><div class="line"> Use `pm2 show &lt;id|name&gt;` to get more details about an app</div><div class="line">dennis@my-remote-server:~$</div></pre></td></tr></table></figure>
<h4 id="设置服务器重启自动启动应用"><a href="#设置服务器重启自动启动应用" class="headerlink" title="设置服务器重启自动启动应用"></a>设置服务器重启自动启动应用</h4><p>比较值得一提的是,通过PM2启动的应用如果发生crash或者被其他程序杀掉了,会自动重新启动。但是需要注意的是,还需要做一点额外的工作,以便在系统重启之后也可以正常的将你的应用程序启动起来。这就是<code>startup</code>命令的作用了:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ pm2 startup systemd</div><div class="line">[PM2] You have to run this <span class="built_in">command</span> as root. Execute the following <span class="built_in">command</span>:</div><div class="line">      sudo su -c <span class="string">"env PATH=<span class="variable">$PATH</span>:/home/dennis/.nvm/versions/node/v6.4.0/bin pm2 startup systemd -u dennis --hp /home/dennis"</span></div></pre></td></tr></table></figure>
<p>执行之后会看到最后一行,是一条命令:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo su -c <span class="string">"env PATH=<span class="variable">$PATH</span>:/home/dennis/.nvm/versions/node/v6.4.0/bin pm2 startup systemd -u dennis --hp /home/dennis"</span></div></pre></td></tr></table></figure></p>
<p>运行一下。成功之后我们可以通过命令<code>sudo systemctl status pm2</code>查看一下状态:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ sudo systemctl status pm2</div><div class="line">● pm2.service - PM2 next gen process manager <span class="keyword">for</span> Node.js</div><div class="line">   Loaded: loaded (/etc/systemd/system/pm2.service; enabled; vendor preset: enabled)</div><div class="line">   Active: active (running) since Fri 2016-08-19 12:02:46 CST; 3min 9s ago</div><div class="line">  Process: 10726 ExecStart=/usr/<span class="built_in">local</span>/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)</div><div class="line"> Main PID: 10732 (PM2 v1.1.3: God)</div><div class="line">   CGroup: /system.slice/pm2.service</div><div class="line">           ├─10732 PM2 v1.1.3: God Daemon</div><div class="line">           └─10742 node /home/dennis/hello.js</div><div class="line"></div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: [PM2] Resurrecting</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: [PM2] Restoring processes located <span class="keyword">in</span> /home/dennis/.pm2/dump.pm2</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: [PM2] Process /home/dennis/hello.js restored</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: │ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: │ hello    │ 0  │ fork │ 10742 │ online │ 0       │ 0s     │ 14.566 MB   │ disabled │</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]: └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘</div><div class="line">Aug 19 12:02:46 my-remote-server pm2[10726]:  Use `pm2 show &lt;id|name&gt;` to get more details about an app</div><div class="line">Aug 19 12:02:46 my-remote-server systemd[1]: Started PM2 next gen process manager <span class="keyword">for</span> Node.js.</div><div class="line">dennis@my-remote-server:~$</div></pre></td></tr></table></figure></p>
<p>关于<code>systemctl</code>命令,强烈建议<a href="https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal" target="_blank" rel="external">看下这个教程</a>。</p>
<h4 id="踩坑提醒-1"><a href="#踩坑提醒-1" class="headerlink" title="踩坑提醒"></a>踩坑提醒</h4><p>如果出现下面的错误:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ sudo su -c <span class="string">"env PATH=<span class="variable">$PATH</span>:/home/dennis/.nvm/versions/node/v6.4.0/bin pm2 startup systemd -u dennis --hp /home/dennis"</span></div><div class="line">[PM2] Generating system init script <span class="keyword">in</span> /etc/systemd/system/pm2.service</div><div class="line">[PM2] Making script booting at startup...</div><div class="line">[PM2] -systemd- Using the <span class="built_in">command</span>:</div><div class="line">      su dennis -c <span class="string">"pm2 dump &amp;&amp; pm2 kill"</span> &amp;&amp; su root -c <span class="string">"systemctl daemon-reload &amp;&amp; systemctl enable pm2 &amp;&amp; systemctl start pm2"</span></div><div class="line">Command failed: su dennis -c <span class="string">"pm2 dump &amp;&amp; pm2 kill"</span> &amp;&amp; su root -c <span class="string">"systemctl daemon-reload &amp;&amp; systemctl enable pm2 &amp;&amp; systemctl start pm2"</span></div><div class="line">/usr/bin/env: <span class="string">'node'</span>: No such file or directory</div><div class="line"></div><div class="line">----- Are you sure you use the right platform <span class="built_in">command</span> line option ? centos / redhat, amazon, ubuntu, gentoo, systemd or darwin?</div></pre></td></tr></table></figure></p>
<p>则可能是因为我们的node是使用nvm安装的原因。为/usr/bin/node添加一个软连接即可。<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo ln -s  /home/dennis/.nvm/versions/<span class="keyword">node</span><span class="title">/v6</span>.<span class="number">4.0</span>/bin/<span class="keyword">node</span> <span class="title">/usr</span>/bin/<span class="keyword">node</span><span class="title"></span></div></pre></td></tr></table></figure></p>
<h4 id="PM2常用命令"><a href="#PM2常用命令" class="headerlink" title="PM2常用命令"></a>PM2常用命令</h4><h5 id="停止应用"><a href="#停止应用" class="headerlink" title="停止应用"></a>停止应用</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 stop app_name_or_id</div></pre></td></tr></table></figure>
<h5 id="重启应用"><a href="#重启应用" class="headerlink" title="重启应用"></a>重启应用</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 restart app_name_or_id</div></pre></td></tr></table></figure>
<h5 id="列举应用"><a href="#列举应用" class="headerlink" title="列举应用"></a>列举应用</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 list</div></pre></td></tr></table></figure>
<h5 id="查看应用详情"><a href="#查看应用详情" class="headerlink" title="查看应用详情"></a>查看应用详情</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 info example</div></pre></td></tr></table></figure>
<h5 id="监控应用"><a href="#监控应用" class="headerlink" title="监控应用"></a>监控应用</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 monit</div></pre></td></tr></table></figure>
<h2 id="使用Nginx作Node-js应用程序的反向代理"><a href="#使用Nginx作Node-js应用程序的反向代理" class="headerlink" title="使用Nginx作Node.js应用程序的反向代理"></a>使用Nginx作Node.js应用程序的反向代理</h2><h3 id="创建Node-js应用程序"><a href="#创建Node-js应用程序" class="headerlink" title="创建Node.js应用程序"></a>创建Node.js应用程序</h3><p>我们已经有了一个<code>hello.js</code>应用,于此类似,再创建一个<code>foo.js</code>的应用。创建文件<code>foo.js</code>,代码如下:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/usr/bin/env nodejs</span></div><div class="line"><span class="keyword">var</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</div><div class="line">http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>) </span>&#123;</div><div class="line">  res.writeHead(<span class="number">200</span>, &#123;<span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span>&#125;);</div><div class="line">  res.end(<span class="string">'Foo Bar\n'</span>);</div><div class="line">&#125;).listen(<span class="number">8081</span>, <span class="string">'localhost'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'Server running at http://localhost:8081/'</span>);</div></pre></td></tr></table></figure></p>
<p>监听端口8081. 启动该应用程序:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm2 start foo.js</div></pre></td></tr></table></figure></p>
<p>这个时候我们看到已经有两个Node.js应用在运行了:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">dennis@my-remote-server:~$ pm2 list</div><div class="line">┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐</div><div class="line">│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │</div><div class="line">├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤</div><div class="line">│ hello    │ 0  │ fork │ 10742 │ online │ 0       │ 46m    │ 21.570 MB   │ disabled │</div><div class="line">│ foo      │ 1  │ fork │ 10897 │ online │ 0       │ 8s     │ 20.051 MB   │ disabled │</div><div class="line">└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘</div><div class="line"> Use `pm2 show &lt;id|name&gt;` to get more details about an app</div></pre></td></tr></table></figure></p>
<h3 id="设置Nginx"><a href="#设置Nginx" class="headerlink" title="设置Nginx"></a>设置Nginx</h3><p>前面我们讲过,Nginx的默认配置文件为/etc/nginx/sites-available/default。我们新建了两个站点,分别对应下面的配置文件:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/etc/nginx/sites-available/example.com</div><div class="line">/etc/nginx/sites-available/test.com</div></pre></td></tr></table></figure></p>
<p>这里我们可以修改默认的配置,也可以选择修改任意一个域名的配置。我们就以<code>example.com</code>为例。<br>将<code>/etc/nginx/sites-available/example.com</code>文件的内容全部删除,并修改内容为:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    listen 80;</div><div class="line"></div><div class="line">    server_name example.com;</div><div class="line"></div><div class="line">    location / &#123;</div><div class="line">        proxy_pass http://localhost:8080;</div><div class="line">        proxy_http_version 1.1;</div><div class="line">        proxy_<span class="built_in">set</span>_header Upgrade <span class="variable">$http_upgrade</span>;</div><div class="line">        proxy_<span class="built_in">set</span>_header Connection <span class="string">'upgrade'</span>;</div><div class="line">        proxy_<span class="built_in">set</span>_header Host <span class="variable">$host</span>;</div><div class="line">        proxy_cache_bypass <span class="variable">$http_upgrade</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>重新加载一下Nginx的配置使其生效:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo systemctl restart nginx</div></pre></td></tr></table></figure></p>
<p>简单解释一下,这个配置的作用就是,监听服务器的80端口,并反向代理到8080端口去,也就是我们的<code>hello</code>应用。也就是说当用户通过浏览器访问<code>http://example.com/</code>的时候,将会将用户请求发给我们的<code>hello.js</code>应用,就会在页面上显示”Hello World”字样。</p>
<h3 id="多应用设置"><a href="#多应用设置" class="headerlink" title="多应用设置"></a>多应用设置</h3><p>如果需要设置对多个Node.js应用的访问,我们可以在Nginx中增加响应的配置即可。以上面创建的<code>foo.js</code>应用为例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">location /app2 &#123;</div><div class="line">        proxy_pass http://localhost:8081;</div><div class="line">        proxy_http_version 1.1;</div><div class="line">        proxy_<span class="built_in">set</span>_header Upgrade <span class="variable">$http_upgrade</span>;</div><div class="line">        proxy_<span class="built_in">set</span>_header Connection <span class="string">'upgrade'</span>;</div><div class="line">        proxy_<span class="built_in">set</span>_header Host <span class="variable">$host</span>;</div><div class="line">        proxy_cache_bypass <span class="variable">$http_upgrade</span>;</div><div class="line">    &#125;</div></pre></td></tr></table></figure></p>
<p>使用命令<code>sudo systemctl restart nginx</code>重启nginx之后,就可以通过访问<code>http://example.com/app2</code>来查看<code>foo.js</code>应用的结果”Foo Bar”了。</p>
<h1 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h1><p>本文只是简单的带着大家配置一台可用的Web服务器,但是必然会有很多知识点没有涉及到。这就需要我们在碰到问题的时候多去Google了。比如下面的Topic,有兴趣的可以自行研究下。</p>
<ol>
<li>Linux/Ubuntu上的一键安装包,有些比较好用,比如<a href="https://github.com/mirzazeyrek/lemp-wordpress-stack" target="_blank" rel="external">lemp-wordpress-stack</a>.</li>
<li>Ubuntu上Docker的安装与使用</li>
<li>使用Let’s Encrypt进行全站HTTPS(可以<a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04" target="_blank" rel="external">参考这篇文章</a>)</li>
<li>科学上网:ubuntu 16.04服务器上搭建Shadowsocks服务(如果大家感兴趣,后边再讲)</li>
<li>同时使用apache和nginx,可以<a href="https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-16-04-server" target="_blank" rel="external">参考这篇文章</a>。</li>
</ol>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference:"></a>Reference:</h2><p><a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04" target="_blank" rel="external">how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file-on-ubuntu-and-centos" target="_blank" rel="external">how-to-edit-the-sudoers-file-on-ubuntu-and-centos</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean" target="_blank" rel="external">how-to-set-up-a-host-name-with-digitalocean</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04" target="_blank" rel="external">how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-16-04-server" target="_blank" rel="external">how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-16-04-server</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04" target="_blank" rel="external">how-to-install-nginx-on-ubuntu-16-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04" target="_blank" rel="external">how-to-install-node-js-on-ubuntu-16-04</a><br><a href="https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal" target="_blank" rel="external">systemd-essentials-working-with-services-units-and-the-journal</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-16-04" target="_blank" rel="external">how-to-install-and-secure-phpmyadmin-on-ubuntu-16-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04" target="_blank" rel="external">how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04</a><br><a href="https://www.digitalocean.com/community/tutorials/lemp-stack-monitoring-with-monit-on-ubuntu-14-04" target="_blank" rel="external">lemp-stack-monitoring-with-monit-on-ubuntu-14-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04" target="_blank" rel="external">how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04</a><br><a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-on-ubuntu-14-04" target="_blank" rel="external">how-to-secure-nginx-on-ubuntu-14-04</a></p>

      
    </div>

    <div>
      
        
<div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/uploads/wechatmp.jpg" alt="令狐葱 wechat" style="width: 200px; max-width: 100%;"/>
    <div>欢迎您扫一扫上面的二维码，订阅我的微信公众号！</div>
</div>


      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div></div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/uploads/wechatpay.png" alt="令狐葱 WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="/uploads/alipaypay.png" alt="令狐葱 Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/前端/" rel="tag"># 前端</a>
          
            <a href="/tags/前端基础/" rel="tag"># 前端基础</a>
          
            <a href="/tags/服务器配置/" rel="tag"># 服务器配置</a>
          
            <a href="/tags/Ubuntu/" rel="tag"># Ubuntu</a>
          
            <a href="/tags/Nginx/" rel="tag"># Nginx</a>
          
            <a href="/tags/LEMP/" rel="tag"># LEMP</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/08/06/nodejs-batch-download-images-with-async-and-bagpipe/" rel="next" title="Nodejs大批量下载图片入坑指南(使用async和bagpipe处理大并发量请求)">
                <i class="fa fa-chevron-left"></i> Nodejs大批量下载图片入坑指南(使用async和bagpipe处理大并发量请求)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/28/introduction-to-react-flux/" rel="prev" title="手把手教你玩转 React Flux">
                手把手教你玩转 React Flux <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/08/18/ubuntu_1604_web_server_config_LEMP/"
           data-title="手把手教你从头搭建Web服务器及LEMP环境(使用Ubuntu16.04+Nginx+MySQL+PHP)" data-url="http://jiji262.github.io/2016/08/18/ubuntu_1604_web_server_config_LEMP/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="令狐葱" />
          <p class="site-author-name" itemprop="name">令狐葱</p>
           
              <p class="site-description motion-element" itemprop="description">去留无意，闲看庭前花开花落；宠辱不惊，漫随天外云卷云舒。</p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">28</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/jiji262" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/linghucong" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/jiji262" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Ubuntu服务器基本设置"><span class="nav-number">2.</span> <span class="nav-text">Ubuntu服务器基本设置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#使用root登录服务器"><span class="nav-number">2.1.</span> <span class="nav-text">使用root登录服务器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#新增超级用户权限用户"><span class="nav-number">2.2.</span> <span class="nav-text">新增超级用户权限用户</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置SSH-Key认证"><span class="nav-number">2.3.</span> <span class="nav-text">设置SSH Key认证</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#禁用密码认证登录方式"><span class="nav-number">2.3.1.</span> <span class="nav-text">禁用密码认证登录方式</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#LEMP环境搭建及配置"><span class="nav-number">3.</span> <span class="nav-text">LEMP环境搭建及配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装Nginx"><span class="nav-number">3.1.</span> <span class="nav-text">安装Nginx</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#踩坑提醒"><span class="nav-number">3.1.1.</span> <span class="nav-text">踩坑提醒</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#安装Nginx时出现错误"><span class="nav-number">3.1.1.1.</span> <span class="nav-text">安装Nginx时出现错误</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#原因分析"><span class="nav-number">3.1.1.1.1.</span> <span class="nav-text">原因分析</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运行apt-get命令更新或安装软件出现Setting-locale-failed错误"><span class="nav-number">3.1.1.2.</span> <span class="nav-text">运行apt-get命令更新或安装软件出现Setting locale failed错误</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#解决办法："><span class="nav-number">3.1.1.2.1.</span> <span class="nav-text">解决办法：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Nginx-常用目录"><span class="nav-number">3.1.2.</span> <span class="nav-text">Nginx 常用目录</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#内容"><span class="nav-number">3.1.2.1.</span> <span class="nav-text">内容</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#服务器配置"><span class="nav-number">3.1.2.2.</span> <span class="nav-text">服务器配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#服务器日志"><span class="nav-number">3.1.2.3.</span> <span class="nav-text">服务器日志</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tips"><span class="nav-number">3.1.3.</span> <span class="nav-text">Tips</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-检测Nginx的配置文件是否有问题："><span class="nav-number">3.1.3.1.</span> <span class="nav-text">1. 检测Nginx的配置文件是否有问题：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-停止apache服务"><span class="nav-number">3.1.3.2.</span> <span class="nav-text">2. 停止apache服务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-取消apache自启动"><span class="nav-number">3.1.3.3.</span> <span class="nav-text">3. 取消apache自启动</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-Nginx启动与停止"><span class="nav-number">3.1.3.4.</span> <span class="nav-text">4. Nginx启动与停止</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-查看本机ip地址"><span class="nav-number">3.1.3.5.</span> <span class="nav-text">5. 查看本机ip地址</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-检查Nginx的状态"><span class="nav-number">3.1.3.6.</span> <span class="nav-text">6. 检查Nginx的状态:</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装MySQL"><span class="nav-number">3.2.</span> <span class="nav-text">安装MySQL</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装"><span class="nav-number">3.2.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置"><span class="nav-number">3.2.2.</span> <span class="nav-text">配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#初始化"><span class="nav-number">3.2.3.</span> <span class="nav-text">初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL的启动和停止"><span class="nav-number">3.2.4.</span> <span class="nav-text">MySQL的启动和停止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试连接"><span class="nav-number">3.2.5.</span> <span class="nav-text">测试连接</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装PHP"><span class="nav-number">3.3.</span> <span class="nav-text">安装PHP</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置PHP设置"><span class="nav-number">3.3.1.</span> <span class="nav-text">配置PHP设置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Nginx中配置支持PHP"><span class="nav-number">3.3.2.</span> <span class="nav-text">Nginx中配置支持PHP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试一下"><span class="nav-number">3.3.3.</span> <span class="nav-text">测试一下</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#安装Wordpress应用程序-可选"><span class="nav-number">4.</span> <span class="nav-text">安装Wordpress应用程序 (可选)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库及环境配置"><span class="nav-number">4.1.</span> <span class="nav-text">数据库及环境配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建数据库及新的数据库用户"><span class="nav-number">4.1.1.</span> <span class="nav-text">创建数据库及新的数据库用户</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#在外网通过IP直接访问操作数据库"><span class="nav-number">4.1.1.1.</span> <span class="nav-text">在外网通过IP直接访问操作数据库</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置Nginx"><span class="nav-number">4.1.2.</span> <span class="nav-text">配置Nginx</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装额外的PHP扩展"><span class="nav-number">4.1.3.</span> <span class="nav-text">安装额外的PHP扩展</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装Wordpress"><span class="nav-number">4.2.</span> <span class="nav-text">安装Wordpress</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#获取并安装wordpress源码"><span class="nav-number">4.2.1.</span> <span class="nav-text">获取并安装wordpress源码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#调整文件权限和所有权"><span class="nav-number">4.2.2.</span> <span class="nav-text">调整文件权限和所有权</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置secure-key"><span class="nav-number">4.2.3.</span> <span class="nav-text">设置secure key</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置数据库信息及文件操作权限"><span class="nav-number">4.2.4.</span> <span class="nav-text">设置数据库信息及文件操作权限</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#LEMP环境上配置多个站点"><span class="nav-number">5.</span> <span class="nav-text">LEMP环境上配置多个站点</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#设置新的文档目录"><span class="nav-number">5.1.</span> <span class="nav-text">设置新的文档目录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#为每个站点创建测试文件"><span class="nav-number">5.2.</span> <span class="nav-text">为每个站点创建测试文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#为每个站点创建server-block文件"><span class="nav-number">5.3.</span> <span class="nav-text">为每个站点创建server block文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#激活两个站点的server-block"><span class="nav-number">5.4.</span> <span class="nav-text">激活两个站点的server block</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#本地测试"><span class="nav-number">5.5.</span> <span class="nav-text">本地测试</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置二级子域名及代理访问"><span class="nav-number">5.6.</span> <span class="nav-text">配置二级子域名及代理访问</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1）添加域名解析"><span class="nav-number">5.6.1.</span> <span class="nav-text">1）添加域名解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2）修改Nginx配置文件"><span class="nav-number">5.6.2.</span> <span class="nav-text">2）修改Nginx配置文件</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Ubuntu上安装和部署Node-js应用"><span class="nav-number">6.</span> <span class="nav-text">Ubuntu上安装和部署Node.js应用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装Node-js"><span class="nav-number">6.1.</span> <span class="nav-text">安装Node.js</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#通过apt-get的方式安装"><span class="nav-number">6.1.1.</span> <span class="nav-text">通过apt-get的方式安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过NVM安装"><span class="nav-number">6.1.2.</span> <span class="nav-text">通过NVM安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过添加PPA的方式安装"><span class="nav-number">6.1.3.</span> <span class="nav-text">通过添加PPA的方式安装</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建一个简单的Node-js应用"><span class="nav-number">6.2.</span> <span class="nav-text">创建一个简单的Node.js应用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用PM2管理Node-js应用"><span class="nav-number">6.3.</span> <span class="nav-text">使用PM2管理Node.js应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装PM2"><span class="nav-number">6.3.1.</span> <span class="nav-text">安装PM2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用PM2"><span class="nav-number">6.3.2.</span> <span class="nav-text">使用PM2</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#启动Node-js应用"><span class="nav-number">6.3.2.1.</span> <span class="nav-text">启动Node.js应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#设置服务器重启自动启动应用"><span class="nav-number">6.3.2.2.</span> <span class="nav-text">设置服务器重启自动启动应用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#踩坑提醒-1"><span class="nav-number">6.3.2.3.</span> <span class="nav-text">踩坑提醒</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#PM2常用命令"><span class="nav-number">6.3.2.4.</span> <span class="nav-text">PM2常用命令</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#停止应用"><span class="nav-number">6.3.2.4.1.</span> <span class="nav-text">停止应用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#重启应用"><span class="nav-number">6.3.2.4.2.</span> <span class="nav-text">重启应用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#列举应用"><span class="nav-number">6.3.2.4.3.</span> <span class="nav-text">列举应用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看应用详情"><span class="nav-number">6.3.2.4.4.</span> <span class="nav-text">查看应用详情</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#监控应用"><span class="nav-number">6.3.2.4.5.</span> <span class="nav-text">监控应用</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用Nginx作Node-js应用程序的反向代理"><span class="nav-number">6.4.</span> <span class="nav-text">使用Nginx作Node.js应用程序的反向代理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建Node-js应用程序"><span class="nav-number">6.4.1.</span> <span class="nav-text">创建Node.js应用程序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置Nginx"><span class="nav-number">6.4.2.</span> <span class="nav-text">设置Nginx</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多应用设置"><span class="nav-number">6.4.3.</span> <span class="nav-text">多应用设置</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#写在最后"><span class="nav-number">7.</span> <span class="nav-text">写在最后</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Reference"><span class="nav-number">7.1.</span> <span class="nav-text">Reference:</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">令狐葱</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"jiji262"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  













  
  

  

  

  

  


</body>
</html>
